package com.conn;

import java.io.*;
import java.util.*;
import java.net.*;

//class ServerWorker implements Runnable {
public class ServerWorker extends Thread {
    Socket s;
    InputStream ins;
    OutputStream ous;

    public ServerWorker(Socket s) {
        this.s = s;

        try {
            ins = s.getInputStream();
            ous = s.getOutputStream();
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("Error: when creating the input stream, the socket is closed,\n"+
                    "the socket is not connected, or the socket input has been\n"+
                    "shutdown using shutdownInput()");
            return ;
        }
    }

    private void dispatchHanler(ParseInfo pi) {
        switch (pi.cmdid) {
            case REGISTER:
                System.out.println("server get REGISTER command");
                RegisterInfo ri = (RegisterInfo)pi.o;
                MessageThreadPool.add(new RegisterMessageHandler((RegisterInfo)pi.o));
                break;
            case LOGIN:
                System.out.println("server hi LOGIN");
                break;
        }
    }

    public void run() {
        byte [] byteBuf = new byte[GlobalData.WRITE_BYTES];
        int recvBytes;

        while (true) {
            try {
                System.out.println("wait request...");
                recvBytes = ins.read(byteBuf, 0, GlobalData.WRITE_BYTES);

                if (-1 == recvBytes) {
                    System.out.println("Error! end of stream has been reached!");
                    return ;
                } else {
                    System.out.print("read byte "+recvBytes+":"+byteBuf.length
                            +" buf:"+new String(byteBuf, "UTF-8"));//String.valueOf(byteBuf));
                    
                    //XmlHeader1 h = new XmlHandler1().getDocHeader(byteBuf, recvBytes);
                    ParseInfo pi = new ParseInfo();
                    if (!Server.getServer().parser.parse(byteBuf, recvBytes, pi)) {
                        System.out.println("parse faile!");
                    } else {
                        dispatchHanler(pi);

                        System.out.println("parser request finished!");
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("Socket broken");
                return ;
            }
            Arrays.fill(byteBuf, (byte)0);
        }
//        try {
//            ins.close();
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }
    }
}

//        System.out.println("name: "+ri.name);
//        System.out.println("sex: "+ri.sex);
//        System.out.println("birthday: "+ri.birthday);
//        System.out.println("phone: "+ri.phone);
//        System.out.println("country: "+ri.country);
//        System.out.println("home: "+ri.home);
//        System.out.println("username: "+ri.username);
